﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BarberShop.BLL;

public partial class MoviePlaySort : BasePage
{
    public int Id
    {
        get
        {
            return string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetPlayList();
            GetData();
            //GetMoviePlayListData();
        }
    }

    protected void btn_search_Click(object sender, EventArgs e)
    {
        //GetMoviePlayListData();
    }

    protected void GetPlayList()
    {
        var data = MoviePalyListBLL.GetMoviePalyList("");
        drp_moviePlayList.DataSource = data;
        drp_moviePlayList.DataTextField = "Name";
        drp_moviePlayList.DataValueField = "id";
        drp_moviePlayList.SelectedValue = Id.ToString();
        drp_moviePlayList.DataBind();
    }

    //getData
    protected void GetData()
    {
        var data = MoviePalyListBLL.GetViewMoviePalyList(Id);
        this.Grid_Movie.DataSource = data;
        this.Grid_Movie.DataKeyNames = new string[] { "PlayListId", "PlayListReId", "playindex" };
        this.Grid_Movie.DataBind();
    }


    protected void Grid_Movie_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.Grid_Movie.PageIndex = e.NewPageIndex;
        GetData();
    }


    protected void Grid_Movie_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow&&e.Row.RowIndex>=0)
        {
            double size = Convert.ToDouble(e.Row.Cells[3].Text.ToString());
            string sizename = "";
            if (size / 1024 < 1024)
            {
                sizename = (size / (double)1024).ToString("N2") + "Kb";
            }
            else if (size / 1024 / 1024 < 1024)
            {
                sizename = (size / (double)1024 / (double)1024).ToString("N2") + "Mb";
            }
            else
            {
                sizename = (size / (double)1024 / (double)1024 / (double)1024).ToString("N2") + "Gb";
            }
            e.Row.Cells[3].Text = sizename;

            int timelength = Convert.ToInt32(e.Row.Cells[4].Text.ToString());
            e.Row.Cells[4].Text = new TimeSpan(0, 0, timelength).ToString();
        }
    }
    protected void Grid_Movie_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.Grid_Movie.EditIndex = e.NewEditIndex;
        GetData();
    }
    protected void Grid_Movie_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.Grid_Movie.EditIndex = -1;
        GetData();
    }
    protected void Grid_Movie_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int NewPlayindex =  Convert.ToInt32(((TextBox)(Grid_Movie.Rows[e.RowIndex].Cells[0].Controls[0])).Text);
        int PlayListId = Convert.ToInt32(Grid_Movie.DataKeys[e.RowIndex]["PlayListId"].ToString());
        int PlayListReId = Convert.ToInt32(Grid_Movie.DataKeys[e.RowIndex]["PlayListReId"].ToString());
        int OldPlayindex = Convert.ToInt32(Grid_Movie.DataKeys[e.RowIndex]["playindex"].ToString());
        MoviePalyListBLL.ModifyPlayIndex(PlayListId, PlayListReId, NewPlayindex, OldPlayindex);
        this.Grid_Movie.EditIndex = -1;
        GetData();
    }
    protected void Grid_Movie_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int PlayListId = Convert.ToInt32(Grid_Movie.DataKeys[e.RowIndex]["PlayListId"].ToString());
        int PlayListReId = Convert.ToInt32(Grid_Movie.DataKeys[e.RowIndex]["PlayListReId"].ToString());
        int OldPlayindex = Convert.ToInt32(Grid_Movie.DataKeys[e.RowIndex]["playindex"].ToString());
        bool r=MovePlayListRelationBLL.DelPlayList(PlayListReId);
        if (r)
            MoviePalyListBLL.UpdatePlayIndex(PlayListId, OldPlayindex);
        GetData();
    }
}